//index.js
//获取应用实例
var app = getApp()
var initdata = function(that) {
  var list = that.data.list
  for (var i = 0; i < list.length; i++) {
    list[i].txtStyle = ""
  }
  that.setData({
    list: list
  })
}
Page({
  data: {
    num: 1,
    hideInformation: true,
    clickAddress: false,
    user: {},
    is_bind: false,
    user_Signup: [],
    clickAddress: '',
    help_status: false,
    isLoginShow: false,
  },
  onShareAppMessage: function() {
    return {
      title: '广汽本田售后服务培训报名系统',
      path: '/pages/index/index'
    }
  },
  onLoad: function() {
    let loginData = wx.getStorageSync('login')
    if (!loginData) {
      this.setData({
        isLoginShow: true
      })
    }
  },
  openDetails: function() {
    if (Object.keys(app._user.we).length === 0) { // 没拿到用户数据时进入获取权限页面 不能进入详情页
      wx.redirectTo({
        url: '/pages/validation/validation',
      })
    } else { // 拿到则点击进入详情
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  onShow: function() {
    var _this = this;
    _this.setData({
      'user': app._user,
      'is_bind': !!app._user.is_bind
    });
    this.GetAllUser();
  },
  // 下拉刷新
  onPullDownRefresh: function() {
    this.setData({
      user_Signup: []
    })
    this.GetAllUser();
  },
  GetAllUser: function() {
    wx.showLoading({
      title: '加载中',
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetSignup',
        openid: _this.data.user.openid
      },
      success: function(res) {
        console.log(res.data)
        if (res.data) {
          _this.setData({
            'user_Signup': res.data
          });
        }
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    });
  },
  delSign: function(e) {
    var spid = e.currentTarget.dataset.id;
    var openid = this.data.user.openid;
    var name = e.currentTarget.dataset.name;
    var _this = this;
    wx.showModal({
      title: '删除提示',
      content: '你确定删除' + name + '的活动么?',
      confirmColor: '#cc0000',
      success: function(res) {
        if (res.confirm) {
          app.showLoadToast();
          wx.request({
            url: app._server + '/Weixin_Hodna/WxLogin.ashx',
            data: {
              method: 'DelSign',
              openid: openid,
              sp_id: spid,
              name: name
            },
            success: function(res) {
              wx.hideLoading()
              if (res.data.DelSign) {
                wx.showModal({
                  title: '提示',
                  content: '删除成功',
                  showCancel: false,
                  confirmColor: '#cc0000',
                  success: function(res) {
                    _this.GetAllUser();
                  }
                });
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon: 'clear',
                  duration: 3000
                });
              }
            }
          });
        }
      }
    })

  },
  openInfor: function(event) {
    var that = this;
    that.setData({
      num: 0,
      hideInformation: false,
      hideProject: true,
    })
  },
  openProject: function(event) {
    var that = this;
    that.setData({
      num: 1,
      hideInformation: true,
      hideProject: false,
    })
  },
  openRegistered: function() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  _toStaffing() {
    wx.navigateTo({
      url: '/pages/staffing/staffing',
    })
  },
  quitLogin() {
    wx.removeStorage({
      key: 'login',
      success: function(res) {
        wx.reLaunch({
          url: '../login/login',
        })
      },
    })
  },
})